package com.city.parking;

import android.app.Activity;
import android.app.Application;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.Toast;

import cn.smssdk.gui.CommonDialog;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.offline.MKOfflineMap;
import com.city.parking.map.OfflineMapService;
import com.city.parking.util.RegistrantList;
import com.city.parking.util.SharePrefUtil;
import com.city.parking.util.Utils;
import com.ubirouting.IndoorMap;
import com.umeng.fb.FeedbackAgent;

public class ParkingApp extends Application {
	public static String mCarId, mPhone;
	public static int mMapType;
	public static int mUid = -1;

    public static final int UNKNOWN_MAP = -1;
    public static final int BAIDU_MAP = 0;
    public static final int GAODE_MAP = 1;
    
    public static final String APPTOKEN = "69ff8ae72232da59a613ecc830ed7c7a";

	// 填写从短信SDK应用后台注册得到的APPKEY
	public static final String APPKEY = "7cebebe707ad";
	// 填写从短信SDK应用后台注册得到的APPSECRET
	public static final String APPSECRET = "7e1961e9fb52bd09966bc4835f5bf1de";

	public static final int NO_NETWORK = 0;
	public static final int MOBILE = 1;
	public static final int WIFI = 2;
	public static int network_status = NO_NETWORK;
	public static final int RQST_CODE_SEARCH = 1;
	public static final int RQST_CODE_ADD_CAR = 2;
	public static final int RQST_CODE_SCAN = 3;
	public static final int RQST_CODE_FAVOR = 4;
	public static final int RQST_CODE_REPORT = 5;

	public static boolean IS_DEBUG = true;

	public MKOfflineMap mOffline = null;
	public String mCurCity = null;
	private static ParkingApp mApp = null;
	public IndoorMap mIndoorMapEnginee;

	private static final String server = "http://120.25.153.123/park";
	public static final String url_insert_user = server + "/insert/user";
	
	public static final String url_insert_carport = server + "/insert/carport";
	public static final String url_carport = server + "/carport/";
	public static final String url_all_carports = server + "/carports";
	public static final String url_get_carports = server + "/specify/carports";

	public static final String url_get_all_parks = server + "/getParks";
	public static final String url_get_park_by_name = server + "/getParkByName";
	public static final String url_get_near_park = server + "/getNearParks";
	public static final String url_update_park_field = server + "/update/parkFields";

	public ParkingApp() {
		mApp = this;
	}

	public static ParkingApp get() {
		if (mApp == null) {
			throw new IllegalStateException("No ParkingApp here!");
		}
		return mApp;
	}

	public static void showToast(String txt) {
		Toast.makeText(mApp, txt, Toast.LENGTH_SHORT).show();
	}

	private Dialog pd;

	public void showProgressBar(Activity activity) {
		if (pd != null && pd.isShowing())
			pd.dismiss();
		pd = CommonDialog.ProgressDialog(activity);
		pd.show();
	}

	public void hideProgressBar() {
		if (pd != null && pd.isShowing())
			pd.dismiss();
	}

	@Override
	public void onCreate() {
		super.onCreate();
		// 在使用 SDK 各组间之前初始化 context 信息，传入 ApplicationContext
		SDKInitializer.initialize(this);

		SharePrefUtil.init(this);
		try {
			mUid = Integer.parseInt(SharePrefUtil.getUsrId());
		} catch (Exception e) {
		}
		mPhone = SharePrefUtil.getPhone();
		mCarId = SharePrefUtil.getCarId();
		mMapType = SharePrefUtil.getMapType();
		if (Utils.isEmptyText(mCarId))
			mCarId = "未添加";

		initNetworkStatus();
		mIndoorMapEnginee = new IndoorMap(this);
	}

	private RegistrantList mNetworkStateRegistrants = new RegistrantList();
	public int mCurrentActiveNetwork = NO_NETWORK;

	public void registerForNetworkState(Handler h, int what, Object obj) {
		mNetworkStateRegistrants.addUnique(h, what, obj);
	}

	public void unRegisterForNetworkState(Handler h) {
		mNetworkStateRegistrants.remove(h);
	}

	public void notifyNetworkStateChanged() {
		Utils.log("notifyNetworkStateChanged");
		mNetworkStateRegistrants.notifyRegistrants();
	}

	private void initNetworkStatus() {
		NetworkInfo network = null;
		State state = null;
		ConnectivityManager connManager = (ConnectivityManager) getApplicationContext()
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		network = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		network_status = NO_NETWORK;
		if (network != null) {
			state = network.getState();
			if (state == State.CONNECTED || state == State.CONNECTING) {
				// MOBILE CONNECTED;
				network_status = MOBILE;
			}
		}
		network = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		if (network != null) {
			state = network.getState();
			if (state == State.CONNECTED || state == State.CONNECTING) {
				// WIFI CONNECTED;
				network_status = WIFI;
			}
		}
		Utils.log("initNetworkStatus, network_status=" + network_status);
		mCurrentActiveNetwork = network_status;
	}
}